package com.erp.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import lombok.Data;

/**
 * @ClassName R
 * @Description TODO
 * @Author songjizhen
 * @Date 2020/6/21 11:18
 * @Version 1.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
 public class R<T> {
     private Integer status;
     private String msg;
    /**
     * data的总条数，在没有确定data类型时初始化为0显然不太合理。
     */
     private Long total;
     private T data;

     private Integer code=0;
     private Long count;

    /**
     *  分组求和之后的数量{1,2,3,1,2,}=3
     */
    private Integer groupCount;

    public static <T> R<T> build(Integer status, String msg, Long total, T data,Integer groupCount) {
        return new R<T>(status, msg, total, data,groupCount);
    }

    public static <T> R<T> build(Integer status, String msg, Long total, T data) {
        return new R<T>(status, msg, total, data);
    }

    public static <T> R<T> build(ResultStatusEnum resultStatusEnum, String msg, Long total, T data) {
        return new R<T>(resultStatusEnum, msg, total, data);
    }

    public static <T> R<T> buildError(ResultStatusEnum resultStatusEnum) {
        return new R<T>(resultStatusEnum);
    }

    public static <T> R<T> build(T data) {
        //long total = ((Page) data).getTotal();
        if(data.getClass().getName().indexOf("Page")==-1){
            return  new R<T>(data);
        }else{
            return new R<T>(((Page) data).getTotal(),data);
        }

    }
    public static <T> R<T> build2(T data) {
        //long total = ((Page) data).getTotal();

            return new R<T>(((Page) data).getTotal(),data);

    }

    public static <T> R<T> buildServer(Long total, T data) {
        return new R<T>(total, data);
    }

    /**
     * 自定义返回状态，信息
     * @param status
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> R<T> buildCustomize(Integer status, String msg) {
        return  new R<T>(status,msg);
    }

    /**
     * 有错误的情况下需要传递错误码 + 错误提示
     *
     * @param status
     * @param msg
     */
    private R(Integer status, String msg) {
        this.status = status;
        this.msg = msg;

        this.count=this.total;
    }

    private R(ResultStatusEnum resultStatusEnum, String msg) {
        this.status = resultStatusEnum.getValue();
        this.msg = msg;

        this.count=this.total;
    }

    private R(ResultStatusEnum resultStatusEnum) {
        this.status = resultStatusEnum.getValue();
        this.msg = resultStatusEnum.getMsg();

        this.count=this.total;
    }

    /**
     * 客户端分页，只需要数据 data
     *
     * @param data
     */
    private R(T data) {
        this.status = ResultStatusEnum.SUCCESS.getValue();
        this.msg = msg;
        this.data = data;

        this.count=this.total;
    }

    /**
     * 服务端分页，data + total
     *
     * @param total
     * @param data
     */
    private R(Long total, T data) {
        this.status = ResultStatusEnum.SUCCESS.getValue();
        this.msg = msg;
        this.total = total;
        this.data = data;

        this.count=this.total;
    }

    private R(Integer status, String msg, Long total, T data) {
        this.status = status;
        this.msg = msg;
        this.total = total;
        this.data = data;

        this.count=this.total;
    }

    private R(ResultStatusEnum resultStatusEnum, String msg, Long total, T data) {
        this.status = resultStatusEnum.getValue();
        this.msg = msg;
        this.total = total;
        this.data = data;

        this.count=this.total;
    }

    /**
     * 主要作用分组之后求和
     * @param status
     * @param msg
     * @param total
     * @param data
     */
    private R(Integer status, String msg, Long total, T data,Integer groupCount) {
        this.status = status;
        this.msg = msg;
        this.total = total;
        this.data = data;
        this.count=this.total;
        this.groupCount=groupCount;
    }


    private R() {}

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public void setCode(int code){
        this.code=code;
    }

    public Integer getGroupCount() {
        return groupCount;
    }

    public void setGroupCount(Integer groupCount) {
        this.groupCount = groupCount;
    }
}
